#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Exeptions.hpp"
#include "menu.hpp"
#include "event.hpp"

violated::menu::menu(sf::RenderWindow* linkedRenderWindow, violated::eventManager* linkedEvM)
{
  BackgroundImg = sf::Image();
  SelectorImg = sf::Image();
  bufferBGM = sf::SoundBuffer();
  LApp = linkedRenderWindow;
  Levm = linkedEvM;

  // init background title
  if (!BackgroundImg.LoadFromFile("title.png"))
    throw DefaultEx("title background image not found");
  Background.SetImage(BackgroundImg);

  //init Selector
  if (!SelectorImg.LoadFromFile("arrow.png"))
    throw DefaultEx("title background image not found");
  Selector.SetImage(SelectorImg);
  Selector.SetPosition(50, 425);

  // init title musique
  if (!bufferBGM.LoadFromFile("gamestart.ogg"))
    throw DefaultEx("BGM sound file not found");
  BGM.SetBuffer(bufferBGM);
  BGM.SetLoop(false);

  //play title music
  titlePlayed = false;
  if (BGM.GetStatus() != 3)
    BGM.Play();
}

void violated::menu::execMenu(void)
{
  sf::Sound::Status soundStatus = BGM.GetStatus();

  LApp->Draw(Background);
  if (soundStatus == sf::Sound::Playing)
    return;
  if (!titlePlayed)
    {
      if (!BackgroundImg.LoadFromFile("resolution.png"))
	throw DefaultEx("title background image not found");
      titlePlayed = true;
    }
  LApp->Draw(Selector);
}
